package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjProductBoomChild;
import com.zb.mapper.ZjProductBoomChildMapper;
import com.zb.result.PageUtil;
import com.zb.service.ViewGoodsService;
import com.zb.service.ZjProductBoomChildService;
import com.zb.service.ZjProductService;
import com.zb.service.ZjSpareService;
import com.zb.view.ViewGoods;
import com.zb.vo.GoodsBoomListVo;
import com.zb.vo.ProductBoomChildListVo;
import com.zb.vo.ProductBoomChildVo;
import com.zb.vo.ProductBoomCountVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 产品boom子件表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-22
 */
@Service
public class ZjProductBoomChildServiceImpl extends ServiceImpl<ZjProductBoomChildMapper, ZjProductBoomChild> implements ZjProductBoomChildService {
    @Resource
    private ZjProductBoomChildMapper boomChildMapper;
    @Resource
    private ViewGoodsService viewGoodsService;
    @Resource
    private ZjProductService productService;
    @Resource
    private ZjSpareService spareService;

    // 新增、修改、删除Boom子件
    @Override
    public void save(Long custAccId, Long productBoomId, List<ProductBoomChildVo> boomChildVos, List<Long> delBoomChildIds) {
        if (null != delBoomChildIds && !delBoomChildIds.isEmpty()) {
            this.removeByIds(delBoomChildIds);
        }
        List<ZjProductBoomChild> adds = new ArrayList<>();
        List<ZjProductBoomChild> updates = new ArrayList<>();
        ZjProductBoomChild child = null;
        for (ProductBoomChildVo vo : boomChildVos) {
            child = new ZjProductBoomChild();
            BeanUtils.copyProperties(vo, child);
            if (null != child.getId()) {
                updates.add(child);
                continue;
            }
            child.setId(IdWorker.getId());
            child.setCustAccId(custAccId);
            child.setProductBoomId(productBoomId);
            adds.add(child);
        }
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
    }

    // 根据产品Boom ID获取产品Boom子件列表
    @Override
    public Map<String, Object> getProductBoomChildList(Long productBoomId, PageUtil pageUtil) {
        PageUtil.setPageLimit(pageUtil);
        QueryWrapper<ZjProductBoomChild> wrapper = new QueryWrapper<>();
        wrapper.select("id", "type", "goods_id", "goods_spec_id", "qty", "remark");
        wrapper.eq("product_boom_id", productBoomId);
        Page<ZjProductBoomChild> page = this.page(new Page<>(pageUtil.getPage(), pageUtil.getLimit()), wrapper);

        List<Long> goodsIds = page.getRecords().stream().map(ZjProductBoomChild::getGoodsId).toList();
        List<Long> goodsSpecIds = page.getRecords().stream().map(ZjProductBoomChild::getGoodsSpecId).toList();
        List<ViewGoods> viewGoodsList = viewGoodsService.getByGoodsIdsAndGoodsSpecIds(goodsIds, goodsSpecIds);
        List<ProductBoomChildListVo> childListVos = page.getRecords().stream().map(vo -> {
            ProductBoomChildListVo childListVo = new ProductBoomChildListVo();
            BeanUtils.copyProperties(vo, childListVo);
            viewGoodsList.stream().filter(f -> Objects.equals(f.getGoodsType(), vo.getType()) && Objects.equals(f.getId(), vo.getGoodsId()) && Objects.equals(f.getSpecId(), vo.getGoodsSpecId()))
                    .findFirst().ifPresent(spareBoom -> {
                        childListVo.setBrand(spareBoom.getBrand());
                        childListVo.setModel(spareBoom.getModel());
                        childListVo.setName(spareBoom.getName());
                        childListVo.setCode(spareBoom.getCode());
                        childListVo.setGoodsType(spareBoom.getType());
                        childListVo.setSpecName(spareBoom.getSpecName());
                        childListVo.setSpecCode(spareBoom.getSpecCode());
                        childListVo.setUnit(spareBoom.getUnit());
                        childListVo.setImage(spareBoom.getImage());
                    });
            return childListVo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), childListVos);
    }

    /**
     * 商品Boom数据封装
     * @param spareBoomList
     * @param vo
     * @param childListVo
     */
    private static void extractedGoodsBoom(List<GoodsBoomListVo> spareBoomList, ZjProductBoomChild vo, ProductBoomChildListVo childListVo) {
        GoodsBoomListVo spareBoom = spareBoomList.stream().filter(pro -> Objects.equals(vo.getGoodsId(), pro.getId()) && Objects.equals(vo.getGoodsSpecId(), pro.getSpecId())).findFirst().orElse(null);
        if (null != spareBoom) {
            childListVo.setBrand(spareBoom.getBrand());
            childListVo.setModel(spareBoom.getModel());
            childListVo.setName(spareBoom.getName());
            childListVo.setCode(spareBoom.getCode());
            childListVo.setGoodsType(spareBoom.getType());
            childListVo.setSpecName(spareBoom.getSpecName());
            childListVo.setSpecCode(spareBoom.getSpecCode());
            childListVo.setUnit(spareBoom.getUnit());
            childListVo.setImage(spareBoom.getImage());
        }
    }

    // 根据产品BoomID获取子件数量
    @Override
    public List<ProductBoomCountVo> getCountByProductBoomIds(List<Long> productBoomIds) {
        return boomChildMapper.getCountByProductBoomIds(productBoomIds);
    }
}
